package day19;

import org.junit.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;

public class ReflectPrc1_1 {
    @Test
    public void getClazz() throws ClassNotFoundException {
        Class<?> s1 = Class.forName("day19.Student");
        Class<?> s2 = Student.class;
        Class<?> s3 = new Student().getClass();
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
    }
    /**方法*/
    @Test
    public void getMs() throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Class<?> s1 = Student.class;
        Method[] methods = s1.getMethods();
//        System.out.println(Arrays.toString(methods));
        for (Method m:methods){
//            System.out.println(m);
            System.out.println(m.getName());
//            System.out.println(m.getClass());
        }
        Method m = s1.getMethod("study");
        Object obj = s1.newInstance();
        m.invoke(obj);
    }
    @Test
    public void getF() throws NoSuchFieldException, InstantiationException, IllegalAccessException {
        Class<?> s1=Student.class;
        Field[] fields = s1.getFields();
        for (Field f:fields){
            System.out.println(f.getName());
            System.out.println(f.getType().getName());
        }
        System.out.println("***********");
        Field field1 = s1.getField("name");
        Field field2 = s1.getField("age");
        Object student1 = s1.newInstance();
        field1.set(student1,"魏和尚");
        field2.set(student1,19);
        System.out.println(field1.get(student1)+","+field2.get(student1));
    }
    /**实例化含参构造*/
    @Test
    public void NewIns() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
       Class<?> s1=Student.class;
        Constructor<?> cons1 = s1.getConstructor(String.class, int.class);
        Object obj = cons1.newInstance("李云龙", 32);
        Student liyunlong=(Student) obj;
        System.out.println(liyunlong.name);
        System.out.println(liyunlong.age);
        liyunlong.study();
    }
    /**暴力反射获取私有成员变量*/
    @Test
    public void getDF() throws NoSuchFieldException, InstantiationException, IllegalAccessException {
        Class<?> s1=Person.class;
        Field field1 = s1.getDeclaredField("name");
        Field field2 = s1.getDeclaredField("age");
        field1.setAccessible(true);
        field2.setAccessible(true);
        Object obj = s1.newInstance();
        field1.set(obj,"赵云");
        field2.set(obj,40);
        System.out.println(field1.get(obj)+","+field2.get(obj)+"岁");
    }
    /**暴力反射获取方法*/
    @Test
    public void getDM() throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Class<?> p1 = Person.class;
        Method m = p1.getDeclaredMethod("find", String.class);
        Constructor<?> dCONs = p1.getDeclaredConstructor(String.class, int.class);
        m.setAccessible(true);
        Object laoli = dCONs.newInstance("老李", 25);
        m.invoke(laoli, "老王");
    }
}
